﻿$(document).ready(function () {

    var tabindex = 1; //start tabindex || 150 is last tabindex
    var $inp = $(':input:enabled:visible:not(disabled):not([readonly]):not(.NormalTextBox)');
    $inp.bind('keydown', function (e) {
        //var key = (e.keyCode ? e.keyCode : e.charCode);
        var key = e.which;
        if (key == 13) {
            iname = $(this).val();
            if (iname != 'ค้นหา' && iname != 'ยกเลิก') {
                tabindex++;
                //while element exist or it's readonly and tabindex not reached max do
                while (($("[TabIndex='" + tabindex + "']").length == 0 || $("[TabIndex='" + tabindex + "']:not([readonly])").length == 0) && tabindex != 150) {
                    tabindex++;
                }
                if (tabindex == 150) { tabindex = 1 } //reseting tabindex if finished
                {
                    $("[TabIndex='" + tabindex + "']").focus()
                }
                return false;
            }
        }
    });

    $("input").focus(function () { //if changing field manualy with click - reset tabindex 
        var input = $(this);
        tabindex = input.attr("tabindex");
    })

});
